#include "E_Meeting.h"


E_Meeting::E_Meeting(void){
}


E_Meeting::~E_Meeting(void){
	peopleList.clear();
}

E_Meeting::E_Meeting(const E_Meeting &meeting):Meeting(meeting){
	this->peopleList = meeting.peopleList;
}

E_Meeting::E_Meeting(float start, float end, const string& subject):Meeting(start,end,subject){
}


// return the list of people
const vector<string>& E_Meeting::getPeopleList() const {
	return this->peopleList;
}

// check if the a person is in a meeting
bool E_Meeting::checkPerson(const string& name) const {
	
	for (unsigned int i = 0; i < peopleList.size(); i++){
		if (peopleList[i] == name){
			return true;
		}
	}
	
	return false;
}

// add a person to the meeting
bool E_Meeting::addPerson(const string& name) {
	
	peopleList.push_back(name);
	
	return true;
}

// remove a person from meeting
bool E_Meeting::removePerson(const string& name){
	
	for (unsigned int i = 0; i < peopleList.size(); i++){
		if (peopleList[i] == name){
			peopleList.erase(peopleList.begin()+i);
			return true;
		}
	}
	
	return false;
}

Type_Code E_Meeting::whoAmI() const {
	return EXTENDED;
}

bool E_Meeting::checkColission(const Meeting& meeting) const {

	if (meeting.whoAmI() == BASE){
		return Meeting::checkColission(meeting);
	}
	
	if (meeting.whoAmI() == EXTENDED){
		E_Meeting &e_meeting = *((E_Meeting*)&meeting);
		if (Meeting::checkColission(meeting)){
			for (unsigned int i = 0; i < this->peopleList.size(); i++){
				for (unsigned int j = 0; j < e_meeting.peopleList.size(); j++){
					if (this->peopleList[i] == e_meeting.peopleList[j]){
						return true;
					}// end of if ==
				}// end of for j
			}// end of for i
		}// end of if checkCollision
	}//end of if whoAmI()
	
	return false;
}

ostream & E_Meeting::toString(ostream & os) const{
	os << this->getTimeStart() << ", " << this->getTimeEnd() << ", " << this->getSubject() << endl;
	os << "participants:";


	for(unsigned int i=0; i < this->getPeopleList().size() ; i++){
		os << this->getPeopleList()[i];
		os << " ";
	}

	os << endl;

	return os;
}


bool E_Meeting::compare(const Meeting & meeting) const {
	if (this->Meeting::compare(meeting)){

			if (meeting.whoAmI()==BASE)
				return true;
			else {
				const E_Meeting * m = (const E_Meeting *) &meeting;
				for(unsigned int i=0; i < this->getPeopleList().size() ; i++){
					if (!(m->checkPerson(this->getPeopleList()[i]))){
						return false;
					}
				}
				return true;
			}
	}

	return false;
}